package com.itany.cms.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 不添加PaginationInnerInterceptor
        // 可以添加其他拦截器，如乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();

        // 启用合理化，当页码超出范围时自动调整到最近的有效页码
        p.setProperty("reasonable", "true");

        // 支持通过Mapper接口参数传递分页参数
        p.setProperty("supportMethodsArguments", "true");

        // 对返回的PageInfo对象进行自动填充
        p.setProperty("returnPageInfo", "check");

        // 允许通过params参数传递自定义count查询SQL
        p.setProperty("params", "count=countSql");

        pageHelper.setProperties(p);
        return pageHelper;
    }
}
